"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M*数据库utc_time函数
Case Name   : 设置timezone参数后，使用utc_time函数
Create At   : 2023.01.17
Owner       : z@wan005
Description :
    1、获取utc时区当前时间
    2、函数UTC_time查询utc时间
    3、清理环境
Expect      :
    1、成功
    2、成功，和时区为utc的时间大致相等
    3、成功
History     :modified by z@wan005,2023.02.13，默认时区时间比utc时区时间快8个小时，utc_time
            因环境存在时间差异，CI时间差不足8小时，为7:59:59，修改断言
            modified by z@wan005,2023.02.15，修改用例，设置时区后查询utc时区的当前
            时间和不设置时区，使用utc_time函数查询的当前时间近似相等
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_utc_timestamp_0001"

    def test_utc_date(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step1:获取utc时区当前时间;expect:成功--'
        self.log.info(text)
        sql_cmd = "set timezone='utc';select current_time;reset timezone;"
        utc_time1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname=f'{self.db_name}')
        self.log.info(utc_time1)

        text = '-step2:函数UTC_time查询utc时间;expect:成功，和时区为utc的时间大致相等-'
        self.log.info(text)
        sql_cmd = "select utc_time;"
        self.log.info(sql_cmd)
        utc_time2 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname=f'{self.db_name}')
        self.log.info(utc_time2)
        self.assertTrue(
            utc_time1.splitlines()[3].strip() <= utc_time2.splitlines()[
                -2].strip(), '执行失败:' + text)

    def tearDown(self):
        text = '----step3:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
